package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//优化 将购物车数据 缓存到 redis中
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("添加购物车")
    @PostMapping("/add")
//    @Cacheable  //首先查看redis中有没有缓存，如果有的话返回缓存数据，没有的话 查询数据库 将数据库数据 放入缓存
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        //添加购物车
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCart(){
            List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCart();
            return Result.success(shoppingCartList);
    }
    @ApiOperation("删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO){

        shoppingCartService.deleteOne(shoppingCartDTO);
        return Result.success();

    }
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result deleteAll(){
        shoppingCartService.deleteAll();
        return Result.success();
    }
}
